/**
 * 统计页面医生js
 * @author lh
 * 2016-01-15
 *
 */
/**
 * 医生数据表格属性配置
 */
//设置表格数据的样式。高度和宽度，刷新页面，样式重新调整
$('#medicalDoctor_eCharts').css('height',orange.mainPage.getHeight());
$('#medicalDoctor_eCharts').css('width',orange.mainPage.getWidth());
//var id="#accord";
//$(id).accordion({
//	onUnselect:function(title,index){
//		var panels = $('#accord').accordion('panels');
//		var t0 = panels[0].panel('options').title;
//		var t1 = panels[1].panel('options').title;
//		if(index==0){
//			$(id).accordion('select',t1);
//		}else{
//			$(id).accordion('select',t0);
//		}
//	}
//});
function md_query(){
	$('#medicalDoctor_Grid').datagrid({
		url:'s/docRes/table',//+'/'+encodeURI(encodeURI("浙江省"))+'/'+encodeURI(encodeURI("杭州市"))+'/'+encodeURI(encodeURI("上城区")),
		queryParams:{
			province:$('#md_province').combobox('getText')=='全国'?'':$('#md_province').combobox('getText'),//'北京市',
			city:$('#md_city').combobox('getText')=='全省'?'':$('#md_city').combobox('getText'),//"北京市",
			dist:$('#md_district').combobox('getText')=='全市'?'':$('#md_district').combobox('getText')//"昌平区"
		},
		loadFilter:orange.systemPage.selfLoadFilter
	});
	md_reLoadOption();
}
	$('#medicalDoctor_Grid').datagrid({ 
		method:'post',
//		url:'s/docRes/table/'+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("东城区")),
		url:'s/docRes/table',
		queryParams:{
			province:'',
			city:"",
			dist:""
		},
		loadFilter:orange.systemPage.selfLoadFilter,
	    singleSelect:true,
	    rownumbers:true,
	    columns:[[
	        {field:'guid',hidden:true},
	        {field:'orgName',title:'中医馆名',width:'13%',sortable:true,halign:'center',formatter:md_checkDetail},
	        {field:'doctorTotalc',title:'医生总数',width:'20%',sortable:true,halign:'center'}, 
	        {field:'doctorTcmc',title:'中医数',width:'13%',sortable:true,halign:'center'},
	        {field:'doctorWest',title:'西医数',width:'13%',sortable:true,halign:'center',formatter:md_option},
	        {field:'doctorTitle0c',title:'主任（含副主任）',width:'13%',sortable:true,halign:'center'},
	        {field:'doctorTitle1c',title:'主治',width:'13%',sortable:true,halign:'center'}, 
	        {field:'doctorTitle2c',title:'住院医师数',width:'13%',sortable:true,halign:'center'}
	    ]],
	    pagination:true,
	    sortable:true,
	    sortName: 'type',
	    sortOrder: 'asc',
	    onLoadSuccess:function(data){
//	    	debugger;
	    }
	});
	//查看执行的sql，只查询了5个字段，doctorTitle0c，1c,2c3个未找到对应关系，没有西医数的字段，做了计算
	//SELECT org.guid,org_name,IFNULL(doctor_totalc, 0) doctor_totalc,IFNULL(doctor_tcmc, 0) doctor_tcmc,
	//IFNULL(doctor_title0c, 0) doctor_title0c,IFNULL(doctor_title1c, 0) doctor_title1c,IFNULL(doctor_title2c, 0) doctor_title2c FROM
	//已知医生总数和中医总数，计算西医数量
	function md_option(value,rows){
		return (rows.doctorTotalc-rows.doctorTcmc);
	}
	//grid link
	//选中的中医馆id,第一次加载该页面是未定义,调用switch isExist的方法，加判断
	function md_checkDetail(value, row, index){
		if(value==null){
			return null;
		}else{
			return '<a href="#" onclick="tcmReloadOrCreate(\''+row.guid+'\',\''+row.orgName+'\')">'+value+'</a>';	
		}
	};
	//查询条件combobox配置
	//省级数据
	var clearData=[];
	var pData=[{'id':1,'text':'全国'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'},{'id':4,'text':'河北省'}];
	$("#md_province").combobox({
		data:pData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
	    	$("#md_province").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	if(node.id==1){
//	    		$("#md_city + .combo").hide();
//	    		$("#md_district + .combo").hide();
//	    		$("#md_city").combobox('setValue','00');
//	    		$("#md_district").combobox('setValue','00');
	    		$("#md_city").combobox({data:clearData});
	    		$("#md_district").combobox({data:clearData});
	    	}else{
//	    		$('#show_md_city')[0].style.display="inline-block";
	    		md_city();//$("#md_city + .combo").show();
	    	}
	    }
	});
	var cData=[{'id':1,'text':'全省'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'}
	,{'id':4,'text':'石家庄市'}];
	$("#md_city").combobox({
//		data:cData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
//	    	$("#md_city").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	if(node.id==1){
	    		$("#md_district").combobox({data:clearData});
//	    		$("#md_district + .combo").hide();
//	    		$("#md_district").combobox('setValue','00');
	    	}else{
//	    		$('#show_md_district')[0].style.display="inline-block";
	    		md_district();//$("#md_town + .combo").show();
	    	}
	    } 
	});
	var tData=[{'id':1,'text':'全市'},{'id':2,'text':'新华区'},{'id':3,'text':'朝阳区'}
	,{'id':4,'text':'西城区'}];
	$("#md_district").combobox({
//		data:tData,   
	    valueField:'id',   
	    textField:'text' ,
	    onLoadSuccess:function(data){
//	    	$("#md_district").combobox('setValue',data[0].id);
	    }
	});
	function md_city(){
		//市级数据
		$("#md_city").combobox({data:cData});
	}
	function md_district(){
		//区域数据
		$("#md_district").combobox({data:tData});
	}
	//页面初始化加载
	$.ajax({
        url:'s/chartDoc',//+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("北京市"))+'/'+encodeURI(encodeURI("东城区")),
        type: 'post',
        data:{
			province:'',
			city:"",
			dist:""
		},
        success: function(data) {
        	var initData=eval(data);
        	var myChart = echarts.init($("#medicalDoctor_eCharts")[0]);
        	var seriesData1=[];//纵轴数据
        	var seriesData2=[];//纵轴数据
        	var xData=[];//横轴数据
        	for (var i=0;i<initData.length;i++){
        		xData.push(initData[i].period);
        		seriesData1.push(initData[i].docTotal);
        		seriesData2.push(initData[i].docTcm);
        	}
        	md_eChart_option(xData,seriesData1,seriesData2,myChart);
        }
    });
	//echarts图表配置
	
	//ajax请求，加载数据
    // 指定图表的配置项和数据
function md_eChart_option(xData,seriesData1,seriesData2,myChart){
	option = {
		grid:{
			show:false,
			bottom :'15%',
			top:'25%',
			height:'65%',
			width:'85%',
			left:'7%',
			right:'8%'
		},
	    tooltip : {
	        trigger: 'axis'
	    },
	    legend: {
	        data:['医生总数','中医医生总数'],//对应serias的顺序，保持一致
	        top:'8%'
	    },
	    calculable : true,
	    xAxis : [
	        {
	            type : 'category',
	            boundaryGap : false, 
	            data : xData
	        }
	    ],
	    yAxis : [
	        {
	            type : 'value'
	        }
	    ],
	    series : [
	        {
	            name:'医生总数',
	            type:'line',
	            data:seriesData1
	        },
	        {
	            name:'中医医生总数',
	            type:'line',
	            data:seriesData2
	        }
	    ]
	};
        // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}

//改变查询条件，折线图重新加载
function md_reLoadOption(){
	$.ajax({
        url:'s/chartDoc',//+encodeURI(encodeURI("浙江省"))+'/'+encodeURI(encodeURI("杭州市"))+'/'+encodeURI(encodeURI("上城区")),
        type: 'post',
        data:{
        	province:$('#md_province').combobox('getText')=='全国'?'':$('#md_province').combobox('getText'),//'北京市',
			city:$('#md_city').combobox('getText')=='全省'?'':$('#md_city').combobox('getText'),//"北京市",
			dist:$('#md_district').combobox('getText')=='全市'?'':$('#md_district').combobox('getText')//"昌平区"
    		},
        success: function(data) {
        	var initData=eval(data);
        	var seriesData1=[];//纵轴数据
        	var seriesData2=[];//纵轴数据
        	var xData=[];//横轴数据
        	var myChart = echarts.init($("#medicalDoctor_eCharts")[0]);
        	for (var i=0;i<initData.length;i++){
        		xData.push(initData[i].period);
        		seriesData1.push(initData[i].docTotal);
        		seriesData2.push(initData[i].docTcm);
        	}
        	md_eChart_option(xData,seriesData1,seriesData2,myChart);
        }
    });
}